package org.phone.manager.services;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.phone.manager.dao.HibernateService;
import org.phone.manager.dao.MySQLOperate;
import org.phone.manager.entity.Product;
import org.phone.manager.entity.ProductType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

@Service
public class ProductService extends MySQLOperate {

	@Autowired
	@Qualifier("categoryService")
	private CategoryService cs;

	// 获得电子产品列表
	public List<Product> getAllProducts() {
		List<Product> list = getAllEntity(Product.class);
		return list;
	}

	// 获取所有一二级类别
	public List<List> getAllType() {
		return cs.getAllTypeName();
	}

	// 根据类别名称获取产品信息
	@SuppressWarnings("unchecked")
	public Set<Product> getProductInfo(Integer id) {
		ProductType productType = (ProductType) getEntity(ProductType.class, id);
		return productType.getProducts();
	}

	// 模糊查询
	public List<Product> getProductInfo(String queryName) {
		String queryString = "from Product p where p.pName like :keyValue";
		List<Product> productListkey = getDao().getCurrentSession().createQuery(queryString)
				.setString("keyValue", "%" + queryName + "%").list();
		return productListkey;
	}

	// 带类别的模糊查询
	public List<Product> getProductInfo(Integer id, String queryName) {
		ProductType productType = (ProductType) getEntity(ProductType.class, id);
		String queryString = "from Product p where p.pName like :keyValue and p.productType=:productType";
		List<Product> productListkey = getDao().getCurrentSession().createQuery(queryString)
				.setString("keyValue", "%" + queryName + "%").setParameter("productType", productType).list();
		return productListkey;
	}

	// 保存或更新产品信息
	public void saveOrUpdatePrduct(Product product, Integer id) {
		ProductType productId = (ProductType) getEntity(ProductType.class, id);
		product.setProductType(productId);
		saveOrUpdateEntity(product);
	}

}
